package com.hst.capacity.application.biz.impl;

import com.hst.capacity.application.biz.DispatchBiz;
import com.hst.capacity.application.biz.ExpressOrderBiz;
import com.hst.capacity.application.biz.IntercityOrderBiz;
import com.hst.capacity.application.biz.SpecialOrderBiz;
import com.hst.capacity.domain.common.constants.Constants;
import com.hst.capacity.domain.model.entity.dispatch.DispatchCityLineEntity;
import com.hst.capacity.domain.model.entity.dispatch.DispatchStationEntity;
import com.hst.capacity.domain.model.entity.driver.DriverEntity;
import com.hst.capacity.domain.model.entity.driver.DriverProfileEntity;
import com.hst.capacity.domain.model.entity.order.express.ExpressOrderEntity;
import com.hst.capacity.domain.model.entity.order.intercity.IntercityOrderEntity;
import com.hst.capacity.domain.model.entity.order.job.JobPlatformEntity;
import com.hst.capacity.domain.model.entity.order.pool.OrderPoolEntity;
import com.hst.capacity.domain.model.entity.setting.line.CityLineEntity;
import com.hst.capacity.domain.model.entity.setting.line.IntercityLineEntity;
import com.hst.capacity.domain.model.entity.setting.station.StationEntity;
import com.hst.capacity.domain.model.entity.vehicle.VehicleEntity;
import com.hst.capacity.domain.model.entity.vehicle.VehicleTypeEntity;
import com.hst.capacity.domain.model.request.dispatch.DispatchDriverParam;
import com.hst.capacity.domain.model.request.dispatch.DispatchParam;
import com.hst.capacity.domain.model.response.dispatch.CityLineVO;
import com.hst.capacity.domain.model.response.dispatch.LineVO;
import com.hst.capacity.domain.model.response.dispatch.StationVO;
import com.hst.capacity.domain.model.response.dispatch.TaskDetailVO;
import com.hst.capacity.domain.model.response.driver.DriverInfoVO;
import com.hst.capacity.domain.model.response.order.OrderVO;
import com.hst.capacity.domain.service.DriverService;
import com.hst.capacity.infra.convertor.DispatchConvertor;
import com.hst.capacity.infra.convertor.DriverConvertor;
import com.hst.capacity.infra.persistence.jpa.driver.DriverProfileRepository;
import com.hst.capacity.infra.persistence.jpa.driver.DriverRepository;
import com.hst.capacity.infra.persistence.jpa.order.express.ExOrderRepository;
import com.hst.capacity.infra.persistence.jpa.order.intercity.IcOrderRepository;
import com.hst.capacity.infra.persistence.jpa.order.job.JobPlatformRepository;
import com.hst.capacity.infra.persistence.jpa.order.pool.OrderPoolRepository;
import com.hst.capacity.infra.persistence.jpa.setting.line.CityLineRepository;
import com.hst.capacity.infra.persistence.jpa.setting.line.IntercityLineRepository;
import com.hst.capacity.infra.persistence.jpa.setting.station.ApplyStationRepository;
import com.hst.capacity.infra.persistence.jpa.setting.station.StationRepository;
import com.hst.capacity.infra.persistence.jpa.vehicle.VehicleRepository;
import com.hst.capacity.infra.persistence.jpa.vehicle.VehicleTypeRepository;
import com.hst.capacity.infra.util.DateUtil;
import com.hst.capacity.infra.util.NumUtil;
import com.hst.capacity.infra.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class DispatchBizImpl implements DispatchBiz {

    @Autowired
    private JobPlatformRepository jobRepository;
    @Autowired
    private StationRepository bseStationRepository;
    @Autowired
    ApplyStationRepository applyStationRepository;
    @Autowired
    private ExOrderRepository exOrderRepository;
    @Autowired
    private CityLineRepository cityLineRepository;
    @Autowired
    private IntercityLineRepository lineRepository;
    @Autowired
    private IcOrderRepository icOrderRepository;
    @Autowired
    private OrderPoolRepository orderPoolRepository;
    @Autowired
    private DriverRepository driverRepository;
    @Autowired
    private DriverProfileRepository driverProfileRepository;
    @Autowired
    private VehicleRepository vehicleRepository;
    @Autowired
    private VehicleTypeRepository vehicleTypeRepository;
    @Autowired
    private DriverService driverService;
    @Autowired
    private ExpressOrderBiz expressOrderBiz;
    @Autowired
    private IntercityOrderBiz intercityOrderBiz;
    @Autowired
    private SpecialOrderBiz specialOrderBiz;

    /**
     * 获取城市的车站列表
     * @param regionCode
     * @return
     */
    @Override
    public DispatchStationEntity getDispatchStationByRegion(String regionCode,String idStation) {
        DispatchStationEntity ds = new DispatchStationEntity();
        //全站点数据取得
        List<StationEntity> lstStation = bseStationRepository.getInnercityStationByCode(regionCode);
        if (lstStation.size()==0){
            return ds;
        }
        if(idStation == null || idStation == ""){
            idStation = lstStation.get(0).getIdStation();
        }
        StationVO selectedStation = new StationVO();
        //全站点数据编辑
        List<StationVO> stationVoList = new ArrayList<>();
        LocalDateTime xMinutesAgo = DateUtil.getXMinutesAgoDateTime(30);

        for (StationEntity station:lstStation){
            StationVO convertStation = DispatchConvertor.INSTANCE.convert( station);
            convertStation.setOrderCount(exOrderRepository.findCountByIdStation(convertStation.getIdStation(),xMinutesAgo));
            stationVoList.add(convertStation);
        }
        List<ExpressOrderEntity> orderList = exOrderRepository.findByIdStation(idStation,xMinutesAgo);
        ds.setOrderList(orderList);
        //ds.setOrderCount((orderList==null ? 0 : orderList.size()));
        int orderCount=0;
        //ds.setOrderCount(exOrderRepository.findCountByIdStation(idStation));
        ds.setOrderSendCount(exOrderRepository.findCountByIdStation(idStation,Constants.Dispatch.In_station.Send,xMinutesAgo));
        ds.setOrderReceiveCount(exOrderRepository.findCountByIdStation(idStation,Constants.Dispatch.In_station.Receive,xMinutesAgo));
        orderCount=ds.getOrderSendCount()+ds.getOrderReceiveCount();
        ds.setOrderCount(orderCount);
        ds.setStationListAll(stationVoList);
        ds.setIdStation(idStation);
        //inStation
        return ds;
    }

    /**
     * 获取指定司机状态
     * 任务信息，订单信息等
     * @param idDriver
     * @return
     */
    @Override
    public TaskDetailVO getExDriverTaskInfo(String idDriver) {

        TaskDetailVO td = new TaskDetailVO();

//        DriverEntity d = driverRepository.findByIdDriver(idDriver);
//        DriverProfileEntity dp = driverProfileRepository.findByIdDriverProfile(d.getIdDriverProfile());
//        VehicleEntity v = vehicleRepository.findByIdVehicle(d.getIdVehicle());
//        VehicleTypeEntity vt = vehicleTypeRepository.findByIdDriver(idDriver);

        DriverInfoVO driverInfo = driverService.driverInfo(idDriver);

        td.setDriver(driverInfo);
        JobPlatformEntity task = jobRepository.findByIdDriver(idDriver);

        if (task!=null){
            td.setTask(DispatchConvertor.INSTANCE.convertDriverTask(task));
            List<ExpressOrderEntity> orderTaskList = exOrderRepository.findByIdDriverAndIdTask(idDriver,task.getIdTask());
            List<ExpressOrderEntity> orderAppointList = exOrderRepository.findAppointOrderByIdDriver(idDriver);
            td.setOrderList(DispatchConvertor.INSTANCE.convertDriverExOrderList(orderTaskList));
            td.setOrderAppointList(DispatchConvertor.INSTANCE.convertDriverExOrderList(orderAppointList));
        }else{
            TaskDetailVO.Task newTask = new TaskDetailVO.Task();
            newTask.setOrderCount(0);
            newTask.setPassengersIncarNumber(0);
            newTask.setSeating(driverInfo.getSeats());
            newTask.setTaskSumMoney(0F);
            List<ExpressOrderEntity> orderAppointList = exOrderRepository.findAppointOrderByIdDriver(idDriver);
            td.setOrderAppointList(DispatchConvertor.INSTANCE.convertDriverExOrderList(orderAppointList));
            td.setTask(newTask);
        }
        return td;
    }

    /**
     * 获取指定司机状态
     * 任务信息，订单信息等
     * @param idDriver
     * @return
     */
    @Override
    public TaskDetailVO getExDriverTaskInfo(String idDriver,String idStation) {

        TaskDetailVO td = new TaskDetailVO();

//        DriverEntity d = driverRepository.findByIdDriver(idDriver);
//        DriverProfileEntity dp = driverProfileRepository.findByIdDriverProfile(d.getIdDriverProfile());
//        VehicleEntity v = vehicleRepository.findByIdVehicle(d.getIdVehicle());
//        VehicleTypeEntity vt = vehicleTypeRepository.findByIdDriver(idDriver);

        DriverInfoVO driverInfo = driverService.driverInfo(idDriver);

        td.setDriver(driverInfo);
        JobPlatformEntity task = jobRepository.findByIdDriver(idDriver);

        if (task!=null){
            td.setTask(DispatchConvertor.INSTANCE.convertDriverTask(task));
            List<ExpressOrderEntity> orderList = exOrderRepository.findByIdDriverAndIdTask(idDriver,task.getIdTask());
            if (orderList!=null){
                td.setOrderList(DispatchConvertor.INSTANCE.convertDriverExOrderList(orderList));
            }
        }else{
            TaskDetailVO.Task newTask = new TaskDetailVO.Task();
            newTask.setOrderCount(0);
            newTask.setPassengersIncarNumber(0);
            newTask.setSeating(driverInfo.getSeats());
            newTask.setTaskSumMoney(0F);
            td.setTask(newTask);
        }
        return td;
    }

    /**
     * 获取城市的车站列表
     * @param regionCode
     * @return
     */
    @Override
    public DispatchCityLineEntity getCityLineByRegion(String regionCode, String idCityLine) {
        DispatchCityLineEntity ds = new DispatchCityLineEntity();
        //全线路数据取得
        List<CityLineEntity> lstCityLine = cityLineRepository.findByCityCode(regionCode);
        if (lstCityLine.size()==0){
            return ds;
        }
        if(idCityLine == null || idCityLine == ""){
            idCityLine = lstCityLine.get(0).getIdCityLine();
        }

        CityLineVO selectedCityLine = new CityLineVO();
        //全站点数据编辑
        List<CityLineVO> cityLineVoList = new ArrayList<>();
        LocalDateTime hoursAgo = DateUtil.getXHoursAgoDateTime(2);
        for (CityLineEntity cityLine:lstCityLine){
            CityLineVO convertCityLine = DispatchConvertor.INSTANCE.convert( cityLine);
            convertCityLine.setOrderCount(icOrderRepository.findCountByIdCityLine(convertCityLine.getIdCityLine(), hoursAgo));
            cityLineVoList.add(convertCityLine);
        }
        List<IntercityOrderEntity> orderList = icOrderRepository.findByIdCityLine(idCityLine, hoursAgo);
        ds.setOrderList(orderList);

        int orderCount=0;
        List<IntercityLineEntity> lstLine = lineRepository.findByIdCityLine(idCityLine);
        if (lstLine != null && lstLine.size() == 2) {
            LineVO goOutOfCityLine = DispatchConvertor.INSTANCE.convert( lstLine.get(0));
            goOutOfCityLine.setOrderCount(icOrderRepository.findCountByIdLine(goOutOfCityLine.getIdLine(), hoursAgo));
            ds.setOrderSend(goOutOfCityLine);
            LineVO returnToCityLine = DispatchConvertor.INSTANCE.convert( lstLine.get(1));
            returnToCityLine.setOrderCount(icOrderRepository.findCountByIdLine(returnToCityLine.getIdLine(), hoursAgo));
            ds.setOrderReceive(returnToCityLine);
            orderCount = goOutOfCityLine.getOrderCount() + returnToCityLine.getOrderCount();
        }
        ds.setOrderCount(orderCount);
        ds.setCityLineListAll(cityLineVoList);
        ds.setIdCityLine(idCityLine);

        return ds;
    }

    /**
     * 获取指定司机状态
     * 任务信息，订单信息等
     * @param idDriver
     * @return
     */
    @Override
    public TaskDetailVO getIcDriverTaskInfo(String idDriver) {

        TaskDetailVO td = new TaskDetailVO();

//        DriverEntity d = driverRepository.findByIdDriver(idDriver);
//        DriverProfileEntity dp = driverProfileRepository.findByIdDriverProfile(d.getIdDriverProfile());
//        VehicleEntity v = vehicleRepository.findByIdVehicle(d.getIdVehicle());
//        VehicleTypeEntity vt = vehicleTypeRepository.findByIdDriver(idDriver);

        DriverInfoVO driverInfo = driverService.driverInfo(idDriver);

        td.setDriver(driverInfo);
        JobPlatformEntity task = jobRepository.findByIdDriver(idDriver);

        if (task!=null){
            td.setTask(DispatchConvertor.INSTANCE.convertDriverTask(task));
            List<IntercityOrderEntity> orderTaskList = icOrderRepository.findByIdDriverAndIdTask(idDriver,task.getIdTask());
            List<IntercityOrderEntity> orderAppointList = icOrderRepository.findAppointOrderByIdDriver(idDriver);
            td.setOrderList(DispatchConvertor.INSTANCE.convertDriverIcOrderList(orderTaskList));
            td.setOrderAppointList(DispatchConvertor.INSTANCE.convertDriverIcOrderList(orderAppointList));
        }else{
            TaskDetailVO.Task newTask = new TaskDetailVO.Task();
            newTask.setOrderCount(0);
            newTask.setPassengersIncarNumber(0);
            newTask.setSeating(driverInfo.getSeats());
            newTask.setTaskSumMoney(0F);
            List<IntercityOrderEntity> orderAppointList = icOrderRepository.findAppointOrderByIdDriver(idDriver);
            td.setOrderAppointList(DispatchConvertor.INSTANCE.convertDriverIcOrderList(orderAppointList));
            td.setTask(newTask);
        }
        return td;
    }

    /**
     * 获取指定司机状态
     * 任务信息，订单信息等
     * @param idDriver
     * @return
     */
    @Override
    public TaskDetailVO getIcDriverTaskInfo(String idDriver,String idCityLine) {

        TaskDetailVO td = new TaskDetailVO();

//        DriverEntity d = driverRepository.findByIdDriver(idDriver);
//        DriverProfileEntity dp = driverProfileRepository.findByIdDriverProfile(d.getIdDriverProfile());
//        VehicleEntity v = vehicleRepository.findByIdVehicle(d.getIdVehicle());
//        VehicleTypeEntity vt = vehicleTypeRepository.findByIdDriver(idDriver);

        DriverInfoVO driverInfo = driverService.driverInfo(idDriver);

        td.setDriver(driverInfo);
        JobPlatformEntity task = jobRepository.findByIdDriver(idDriver);
//        IntercityDriverTaskEntity task = icDriverTaskRepository.findByIdDriverAndTaskType(idDriver,Constants.OrderType.REALTIME);
        if (task!=null){
            td.setTask(DispatchConvertor.INSTANCE.convertDriverTask(task));
            List<IntercityOrderEntity> orderList = icOrderRepository.findByIdDriverAndIdTask(idDriver,task.getIdTask());
            if (orderList!=null){
                td.setOrderList(DispatchConvertor.INSTANCE.convertDriverIcOrderList(orderList));
            }
        }
        return td;
    }

    /**
     * 获取指定司机状态
     * 任务信息，订单信息等
     * @param idDriver
     * @return
     */
    @Override
    public TaskDetailVO refreshDriverTaskInfo(String idDriver) {

        TaskDetailVO td = new TaskDetailVO();

//        DriverEntity d = driverRepository.findByIdDriver(idDriver);
//        DriverProfileEntity dp = driverProfileRepository.findByIdDriverProfile(d.getIdDriverProfile());
//        VehicleEntity v = vehicleRepository.findByIdVehicle(d.getIdVehicle());
//        VehicleTypeEntity vt = vehicleTypeRepository.findByIdDriver(idDriver);

        DriverInfoVO driverInfo = driverService.driverInfo(idDriver);

        td.setDriver(driverInfo);
        JobPlatformEntity task = jobRepository.findByIdDriver(idDriver);
        //DriverExPlatformEntity task = exDriverTaskRepository.findByIdDriverAndTaskType(idDriver,Constants.OrderType.REALTIME);
        if (task!=null){
            switch (task.getModuleType()) {
                case Constants.OrderModuleType.SPECIAL:
                    List<OrderPoolEntity> orderListSp = orderPoolRepository.findByIdDriverAndIdTask(idDriver,task.getIdTask());
                    if (orderListSp!=null){
                        td.setOrderList(DispatchConvertor.INSTANCE.convertDriverSpOrderList(orderListSp));
                        task = doRefreshDriverTaskSp(task,orderListSp);
                    }
                    break;
                case Constants.OrderModuleType.INTERCITY:
                    List<IntercityOrderEntity> orderListIc = icOrderRepository.findByIdDriverAndIdTask(idDriver,task.getIdTask());
                    if (orderListIc!=null){
                        td.setOrderList(DispatchConvertor.INSTANCE.convertDriverIcOrderList(orderListIc));
                        task = doRefreshDriverTaskIc(task,orderListIc);
                    }
                    break;
                case Constants.OrderModuleType.EXPRESS:
                    List<ExpressOrderEntity> orderListEx = exOrderRepository.findByIdDriverAndIdTask(idDriver,task.getIdTask());
                    if (orderListEx!=null){
                        td.setOrderList(DispatchConvertor.INSTANCE.convertDriverExOrderList(orderListEx));
                        task = doRefreshDriverTaskEx(task,orderListEx);
                    }
                    break;
            }

            td.setTask(DispatchConvertor.INSTANCE.convertDriverTask(task));

        }else{
            TaskDetailVO.Task newTask = new TaskDetailVO.Task();
            newTask.setOrderCount(0);
            newTask.setPassengersIncarNumber(0);
            newTask.setSeating(driverInfo.getSeats());
            newTask.setTaskSumMoney(0F);
            td.setTask(newTask);
        }
        return td;
    }

    @Override
    public int dispatchReminder(String idFranchisee) {
        int remindNum = 0;

        LocalDateTime remindTime = DateUtil.getXHoursAgoDateTime(-2);

        List<ExpressOrderEntity> exLst = exOrderRepository.orderReminder(idFranchisee, remindTime);
        remindNum = remindNum + (exLst == null ? 0 : exLst.size());

        List<IntercityOrderEntity> icLst = icOrderRepository.orderReminder(idFranchisee, remindTime);
        remindNum = remindNum + (icLst == null ? 0 : icLst.size());

        List<OrderPoolEntity> orderPoolLst = orderPoolRepository.orderReminder(idFranchisee, remindTime);
        remindNum = remindNum + (orderPoolLst == null ? 0 : orderPoolLst.size());

        return remindNum;
    }

    @Override
    public boolean reDispatchDriver(DispatchDriverParam param) {
        String idOrder = param.getIdOrder();
        int moduleType = StringUtil.orderType(idOrder);
        Boolean result = false;

        switch (moduleType) {
            case Constants.OrderModuleType.EXPRESS:
                result = expressOrderBiz.reDispatchDriver(param);
                break;
            case Constants.OrderModuleType.INTERCITY:
                result = intercityOrderBiz.reDispatchDriver(param);
                break;
            case Constants.OrderModuleType.SPECIAL:
                result = specialOrderBiz.reDispatchDriver(param);
                break;
        }

        return result;
    }

    @Override
    public DispatchStationEntity findStationList(DispatchParam param) {
        String regionCode = param.getRegionCode();
        String idStation = param.getIdStation();
        DispatchStationEntity ds = new DispatchStationEntity();
        //全站点数据取得
        List<StationEntity> lstStation = bseStationRepository.getInnercityStationByCode(regionCode);
        if (lstStation.size()==0){
            return ds;
        }
        if(idStation == null || idStation == ""){
            idStation = lstStation.get(0).getIdStation();
        }
        StationVO selectedStation = new StationVO();
        //全站点数据编辑
        List<StationVO> stationVoList = new ArrayList<>();
        LocalDateTime appointmentTimeFrom = DateUtil.parseLocalDateTime(param.getAppointmentTimeFrom());
        LocalDateTime appointmentTimeTo = DateUtil.parseLocalDateTime(param.getAppointmentTimeTo());
//        LocalDateTime xMinutesAgo = DateUtil.getXMinutesAgoDateTime(30);
        for (StationEntity station:lstStation){
            StationVO convertStation = DispatchConvertor.INSTANCE.convert( station);
            convertStation.setOrderCount(exOrderRepository.findCountByIdStation(convertStation.getIdStation(),appointmentTimeFrom,appointmentTimeTo));
            stationVoList.add(convertStation);
        }
        List<ExpressOrderEntity> orderList = exOrderRepository.findByIdStation(idStation,appointmentTimeFrom,appointmentTimeTo);
        ds.setOrderList(orderList);
        //ds.setOrderCount((orderList==null ? 0 : orderList.size()));
        int orderCount=0;
        //ds.setOrderCount(exOrderRepository.findCountByIdStation(idStation));
        ds.setOrderSendCount(exOrderRepository.findCountByIdStation(idStation,Constants.Dispatch.In_station.Send,appointmentTimeFrom,appointmentTimeTo));
        ds.setOrderReceiveCount(exOrderRepository.findCountByIdStation(idStation,Constants.Dispatch.In_station.Receive,appointmentTimeFrom,appointmentTimeTo));
        orderCount=ds.getOrderSendCount()+ds.getOrderReceiveCount();
        ds.setOrderCount(orderCount);
        ds.setStationListAll(stationVoList);
        ds.setIdStation(idStation);
        //inStation
        return ds;
    }

    @Override
    public DispatchCityLineEntity findCityLineList(DispatchParam param) {
        String regionCode = param.getRegionCode();
        String idCityLine = param.getIdCityLine();
        DispatchCityLineEntity ds = new DispatchCityLineEntity();
        //全线路数据取得
        List<CityLineEntity> lstCityLine = cityLineRepository.findByCityCode(regionCode);
        if (lstCityLine.size()==0){
            return ds;
        }
        if(idCityLine == null || idCityLine == ""){
            idCityLine = lstCityLine.get(0).getIdCityLine();
        }
        CityLineVO selectedCityLine = new CityLineVO();
        //全站点数据编辑
        List<CityLineVO> cityLineVoList = new ArrayList<>();

        LocalDateTime appointmentTimeFrom = DateUtil.parseLocalDateTime(param.getAppointmentTimeFrom());
        LocalDateTime appointmentTimeTo = DateUtil.parseLocalDateTime(param.getAppointmentTimeTo());

        for (CityLineEntity cityLine:lstCityLine){
            CityLineVO convertCityLine = DispatchConvertor.INSTANCE.convert(cityLine);
            convertCityLine.setOrderCount(icOrderRepository.findCountByIdCityLine(convertCityLine.getIdCityLine(),appointmentTimeFrom,appointmentTimeTo));
            cityLineVoList.add(convertCityLine);
        }

        List<IntercityOrderEntity> orderList = icOrderRepository.findByIdCityLine(idCityLine,appointmentTimeFrom,appointmentTimeTo);
        ds.setOrderList(orderList);
        //ds.setOrderCount((orderList==null ? 0 : orderList.size()));
        int orderCount=0;
        List<IntercityLineEntity> lstLine = lineRepository.findByIdCityLine(idCityLine);
        if (lstLine!=null && lstLine.size()==2){
            LineVO convertLine0 = DispatchConvertor.INSTANCE.convert( lstLine.get(0));
            int line0 = icOrderRepository.findCountByIdLine(convertLine0.getIdLine(),appointmentTimeFrom,appointmentTimeTo);
            convertLine0.setOrderCount(line0);
            ds.setOrderSend(convertLine0);
            LineVO convertLine1 = DispatchConvertor.INSTANCE.convert( lstLine.get(1));
            int line1 = icOrderRepository.findCountByIdLine(convertLine1.getIdLine(),appointmentTimeFrom,appointmentTimeTo);
            convertLine1.setOrderCount(line1);
            ds.setOrderReceive(convertLine1);
            orderCount= line0 + line1;
        }
        ds.setOrderCount(orderCount);
        ds.setCityLineListAll(cityLineVoList);
        ds.setIdCityLine(idCityLine);
        return ds;
    }

    /**
     * 重置任务信息（快车）
     * @param task
     * @param orderList
     * @return
     */
    private JobPlatformEntity doRefreshDriverTaskEx(JobPlatformEntity task,List<ExpressOrderEntity> orderList){
        Integer passengers_incar_number = 0;
        Float taskSumMoney = 0f;
        Float taskAdditionalMoney = 0f;
        Float task_coupon_money = 0f;
        for (ExpressOrderEntity row:orderList) {
            // 在乘人数(已在车上人数+将要乘车人数，以下车人数不在内）+ 当前派单的订单人数
            passengers_incar_number  = passengers_incar_number + row.getPassengerNumber();
            // 预估金额
            taskSumMoney = taskSumMoney + NumUtil.getFloat(row.getEstimatedMoney());
            // 任务额外金额
            taskAdditionalMoney = taskAdditionalMoney + NumUtil.getFloat(row.getAdditionalMoney());
            // 任务优惠金额
            task_coupon_money = task_coupon_money + NumUtil.getFloat(row.getCouponDiscount());
        }
        task.setPassengersIncarNumber(passengers_incar_number);
        task.setTaskSumMoney(taskSumMoney);
        task.setTaskAdditionalMoney(taskAdditionalMoney);
        task.setTaskCouponMoney(task_coupon_money);
        task.setUpdateBy(Constants.Def_CreateBy);
        jobRepository.saveAndFlush(task);
        return task;
    }

    /**
     * 重置任务信息（城际）
     * @param task
     * @param orderList
     * @return
     */
    private JobPlatformEntity doRefreshDriverTaskIc(JobPlatformEntity task,List<IntercityOrderEntity> orderList){
        Integer passengers_incar_number = 0;
        Float taskSumMoney = 0f;
        Float taskAdditionalMoney = 0f;
        Float task_coupon_money = 0f;
        for (IntercityOrderEntity row:orderList) {
            // 在乘人数(已在车上人数+将要乘车人数，以下车人数不在内）+ 当前派单的订单人数
            passengers_incar_number  = passengers_incar_number + row.getPassengerNumber();
            // 预估金额
            taskSumMoney = taskSumMoney + NumUtil.getFloat(row.getEstimatedMoney());
            // 任务额外金额
            taskAdditionalMoney = taskAdditionalMoney + NumUtil.getFloat(row.getAdditionalMoney());
            // 任务优惠金额
            task_coupon_money = task_coupon_money + NumUtil.getFloat(row.getCouponDiscount());
        }
        task.setPassengersIncarNumber(passengers_incar_number);
        task.setTaskSumMoney(taskSumMoney);
        task.setTaskAdditionalMoney(taskAdditionalMoney);
        task.setTaskCouponMoney(task_coupon_money);
        task.setUpdateBy(Constants.Def_CreateBy);
        jobRepository.saveAndFlush(task);
        return task;
    }

    /**
     * 重置任务信息（订单池）
     * @param task
     * @param orderList
     * @return
     */
    private JobPlatformEntity doRefreshDriverTaskSp(JobPlatformEntity task,List<OrderPoolEntity> orderList){
        Integer passengers_incar_number = 0;
        Float taskSumMoney = 0f;
        Float taskAdditionalMoney = 0f;
        Float task_coupon_money = 0f;
        for (OrderPoolEntity row:orderList) {
            // 在乘人数(已在车上人数+将要乘车人数，以下车人数不在内）+ 当前派单的订单人数
            passengers_incar_number  = passengers_incar_number + row.getPassengerNumber();
            // 预估金额
            taskSumMoney = taskSumMoney + NumUtil.getFloat(row.getEstimatedMoney());
            // 任务额外金额
            taskAdditionalMoney = taskAdditionalMoney + NumUtil.getFloat(row.getAdditionalMoney());
            // 任务优惠金额
            task_coupon_money = task_coupon_money + NumUtil.getFloat(row.getCouponDiscount());
        }
        task.setPassengersIncarNumber(passengers_incar_number);
        task.setTaskSumMoney(taskSumMoney);
        task.setTaskAdditionalMoney(taskAdditionalMoney);
        task.setTaskCouponMoney(task_coupon_money);
        task.setUpdateBy(Constants.Def_CreateBy);
        jobRepository.saveAndFlush(task);
        return task;
    }

}
